package com.app.service.impl;

import com.app.entity.Course;
import com.app.entity.CourseCollection;
import com.app.mapper.CourseCollectionMapper;
import com.app.service.CourseCollectionService;
import com.app.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yeyuan
 * @since 2021-12-30
 */
@Service
public class CourseCollectionServiceImpl extends ServiceImpl<CourseCollectionMapper, CourseCollection> implements CourseCollectionService {

    @Autowired
    private CourseService courseService;
    @Override
    public boolean getIsCollected(Long cid, Long uid) {
        LambdaQueryWrapper<CourseCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(cid),CourseCollection::getCid,cid);
        wrapper.eq(ObjectUtils.isNotNull(uid),CourseCollection::getUid,uid);

        return getBaseMapper().selectList(wrapper).size()>0;
    }

    @Override
    public boolean collect(Long cid, Long uid) {

        return getBaseMapper().insert(new CourseCollection(cid,uid))>0;
    }

    @Override
    public boolean cancelCollect(Long cid, Long uid) {
        LambdaQueryWrapper<CourseCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(cid),CourseCollection::getCid,cid);
        wrapper.eq(ObjectUtils.isNotNull(uid),CourseCollection::getUid,uid);
        return getBaseMapper().delete(wrapper)>0;
    }

    @Override
    public List<Course> getCollectedListByUid(Long uid) {
        LambdaQueryWrapper<CourseCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(uid),CourseCollection::getUid,uid);
        List<CourseCollection> courseCollections = getBaseMapper().selectList(wrapper);
       return courseService.getBaseMapper().selectBatchIds(courseCollections.stream().map(CourseCollection::getCid).collect(Collectors.toList()));

    }

}
